<?php
/**
 * Created by JetBrains PhpStorm.
 * User: T420
 * Date: 9/27/12
 * Time: 3:58 PM
 * To change this template use File | Settings | File Templates.
 */
class Controllers_Index extends Libs_Controller
{

    public function __construct()
    {
        parent::__construct();
//        $this->menu=new Models_Menu();
//        $this->getmenu();
        $this->model=new Models_Index();
        new Controllers_Menu();
    }
//    public function getmenu()
//    {
//
//        $this->view->listmenusys=$this->menu->getmenusystem();
//        $this->view->listmenuproduct=$this->menu->getmenuproduct();
//        $this->view->listmanufacture=$this->menu->getmanufactory();
//        $this->view->listnews=$this->menu->getnews();
//    }
    public function index()
    {

        $this->view->phutungmay=$this->model->getproduct(1);
        $this->view->phutunggam=$this->model->getproduct(2);
        $this->view->phutungvo=$this->model->getproduct(3);
        $this->view->phutungdien=$this->model->getproduct(4);
        $this->view->render('index/index');
    }
    public function login(){
        if(isset($_SESSION['username'])&&$_SESSION['username']!='')
        {
            echo '<script type="text/javascript">history.back();</script>';
        }
        else
        $this->view->render('index/login');
    }
    public function processLogin()
    {
//        if(isset($_SESSION['username'])&&$_SESSION['username']!='')
//        {
//            echo '<script type="text/javascript">history.back();</script>';
//        }
//        else
//        {
            $username=$_POST['username'];
            $pass=$_POST['password'];
            $check=new Models_Login();
            $listuser=$check->checklogin($username,$pass);
            if(count($listuser)>0)
            {
                foreach($listuser as $key)
                {
                    $_SESSION['userid']=$key->userid;
                    $_SESSION['username']=$key->username;


                }

             echo '<script>history.back()
            </script>';
            }
            else{
                $this->login();
                echo '<script type="text/javascript">alert("Sai thông tin đăng nhập! Mời bạn đăng nhập lại");</script>';
                echo '<script>history.back()
            </script>';
            }
//        }




    }
    public function register()
    {

        $this->view->render('index/register');
    }
    public function processRegister()
    {
        $username=$_POST['username'];
        $password=$_POST['password'];
        $fullname=$_POST['fullname'];
        //$gender=$_POST['gender'];
        $birthday=$_POST['birthday'];
        $phone=$_POST['phone'];
        $email=$_POST['email'];
        $address=$_POST['address'];
        //echo $username;

        $insert=new Models_Register();
        $insert->insertuser($username,$password,$fullname,$birthday,$phone,$email,$address);

        $this->view->render('register/process');
    }
    public function logout()
    {

        $_SESSION['username']='';
        $_SESSION['grade']='';
        echo '<script>location="'.PATH.'";
            </script>';
    }
    public function replacepass()
    {
        if($_SESSION['username']!='')
        {

            $model=new Models_Login();
            $this->view->infouser=$model->getinfouser($_SESSION['username']);
            $this->view->render('index/replacepass');

            if($_POST['submit']!='')
            {
                $oldpass=$_POST['oldpassword'];
                $newpass=$_POST['newpassword'];
                foreach($this->view->infouser as $value)
                {
                    if($value->password!=md5($oldpass))
                    {
                        echo '<script type="text/javascript">alert("Mật khẩu cũ không đúng");
                        frm_replacePass.oldpassword.focus();
                        </script>';
                    }

                    else{
                        $model->changepass($value->userid,$newpass);
                        echo '<script type="text/javascript">alert("Đổi mật khẩu thành công!!");

                        </script>';

                    }
                }

                //}
            }

        }

        else
            echo 'You have not permisson';
    }

    public function about()
    {
        $this->view->render('about/index');
    }

}
